/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJDialog.java
 *
 * Created on Jun 2, 2011, 10:19:47 AM
 */

/**
 *
 * @author cakridge
 */
import java.awt.Frame;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.text.Position.Bias;

public class GraphDialog extends javax.swing.JDialog {
  private String[] graphVarNames;
  private String inputFile;
  protected DefaultListModel listModelYvalue;
  private DefaultListModel listModelXvalue;
  protected NewResultsFrame newResultsFrame;
    /** Creates new form NewJDialog */
    public GraphDialog(java.awt.Frame parent, boolean modal, String[] graphVarNames, NewResultsFrame newResultsFrame) {
        super(parent, modal);
        this.newResultsFrame = newResultsFrame;
        //Contains variable names to select the x and y values for the graph.
        this.graphVarNames = new String[1000];
        System.arraycopy(graphVarNames, 0, this.graphVarNames, 0, graphVarNames.length); // place the input variables into the array first
        /** Contains the values passed to the user-defined fitness function for
         * each computation (ie. evolution).
         */
        listModelXvalue = new DefaultListModel();
        listModelYvalue = new DefaultListModel();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    yValuesAvail = new javax.swing.JComboBox(graphVarNames);
    xValuesAvail = new javax.swing.JComboBox(graphVarNames);
    jButton1AddItem = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    yValuesAdded = new javax.swing.JList();
    jButton2AddItem = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    xValueAdded = new javax.swing.JTextField();
    jButton3_plot = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jButton5RemoveItem = new javax.swing.JButton();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosed(java.awt.event.WindowEvent evt) {
        formWindowClosed(evt);
      }
    });

    jButton1AddItem.setText("-->");
    jButton1AddItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1AddItemActionPerformed(evt);
      }
    });
    yValuesAdded.setModel(listModelYvalue);

    /*
    */
    jScrollPane1.setViewportView(yValuesAdded);

    jButton2AddItem.setText("-->");
    jButton2AddItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2AddItemActionPerformed(evt);
      }
    });

    jLabel1.setText("Select Y-Values");

    jLabel2.setText("Select X-Value");

    xValueAdded.setEditable(false);
    xValueAdded.setText("Evaluation");
    xValueAdded.setActionCommand("<Not Set>");

    jButton3_plot.setText("Plot");
    jButton3_plot.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton3_plotActionPerformed(evt);
      }
    });

    jButton4.setText("Cancel");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        CloseButtonActionPerformed(evt);
      }
    });

    jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 18));
    jLabel3.setText("Create New Plot Tab");

    jLabel4.setText("Y-Values Added");

    jButton5RemoveItem.setText("<--");
    jButton5RemoveItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton5RemoveItemActionPerformed(evt);
      }
    });

    jButton1.setText("Help");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap(20, Short.MAX_VALUE)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
              .add(layout.createSequentialGroup()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton3_plot))
              .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                  .add(layout.createSequentialGroup()
                    .add(39, 39, 39)
                    .add(jLabel2))
                  .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(xValuesAvail, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(yValuesAvail, 0, 166, Short.MAX_VALUE))
                      .add(layout.createSequentialGroup()
                        .add(43, 43, 43)
                        .add(jLabel1)))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                      .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel3))
                      .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                          .add(jButton5RemoveItem)
                          .add(jButton1AddItem)
                          .add(org.jdesktop.layout.GroupLayout.LEADING, jButton2AddItem))))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                  .add(xValueAdded)
                  .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))))
            .addContainerGap())
          .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .add(jLabel4)
            .add(84, 84, 84))))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(jLabel3)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jLabel4)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 116, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(layout.createSequentialGroup()
            .add(17, 17, 17)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(yValuesAvail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
              .add(layout.createSequentialGroup()
                .add(jButton1AddItem)
                .add(20, 20, 20)
                .add(jButton5RemoveItem)))
            .add(44, 44, 44)
            .add(jLabel2)))
        .add(9, 9, 9)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
              .add(xValuesAvail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
              .add(xValueAdded, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(26, 26, 26)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
              .add(jButton3_plot)
              .add(jButton4)
              .add(jButton1)))
          .add(jButton2AddItem))
        .add(60, 60, 60))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButton3_plotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3_plotActionPerformed
      setVisible(false);
 //     this.newResultsFrame.resultsFrame.monitor.addGraph(null);
      this.newResultsFrame.resultsFrame.monitor.addGraph(this.newResultsFrame.resultsFrame.monitor.graphs);

      this.newResultsFrame.resultsFrame.monitor.graphs.getLast().newGraph(this.newResultsFrame.jTabbedPane1, getyValuesAdded(), getxValueAdded());
    }//GEN-LAST:event_jButton3_plotActionPerformed

    private void jButton1AddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1AddItemActionPerformed
      // This is where you add the y-value to the list
      int iDx;
      Object o = yValuesAvail.getSelectedItem();
      if(firstItemAdded == true) {
        iDx = yValuesAdded.getNextMatch(o.toString(), 0, Bias.Backward);
      } else {
        firstItemAdded = true;
        iDx = -1;
      }
      if(iDx == -1) { // add to list
        int pos = yValuesAdded.getModel().getSize();
        listModelYvalue.insertElementAt(o, pos);
      } else { // if it's not in list then print error
       // print error message
       if(duplicateNameDialog == null) { // get a new error window
         java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  Error_duplicate_name duplicateNameDialog = new Error_duplicate_name(new javax.swing.JFrame(), true);
              }
          });
        } else { // make the existing box visible
         duplicateNameDialog.setVisible(true);
        }
    }//GEN-LAST:event_jButton1AddItemActionPerformed
  }
    private void jButton5RemoveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5RemoveItemActionPerformed
      int i = yValuesAdded.getSelectedIndex();
    Object remove = listModelYvalue.remove(i);
    }//GEN-LAST:event_jButton5RemoveItemActionPerformed

    private void jButton2AddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2AddItemActionPerformed
      // This is where you add the y-value to the list
      Object o = xValuesAvail.getSelectedItem();
      //int pos = jTextField1.getModel().getSize();
      xValueAdded.setText((String)o);
    }//GEN-LAST:event_jButton2AddItemActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
      // close duplicateNameDialog box
      setVisible(false);
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
      setVisible(false);
    }//GEN-LAST:event_formWindowClosed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
 //               GraphDialog duplicateNameDialog = new GraphDialog(new javax.swing.JFrame(), true);
//                duplicateNameDialog.addWindowListener(new java.awt.event.WindowAdapter() {
//          @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                duplicateNameDialog.setVisible(true);
            }
        });
    }
public String getxValueAdded() {
 // Object tmp = listModelXvalue.firstElement();
  String text = xValueAdded.getText();
  if(text == null) {
    return("numGenerations");
  } else {
    return (text);
  }
}
public LinkedList<String> getyValuesAdded() {
//  ArrayList<String> stringSel = new ArrayList<String>();
  LinkedList<String> str = new LinkedList<String>();

  // Get the index of all the selected items
  int[] selectedIx = yValuesAdded.getSelectedIndices();
    // Get all the selected items using the indices
    int size = listModelYvalue.getSize();
  for (int i=0; i<size; i++) {
    Object element = listModelYvalue.get(i);
   // Object sel = yValuesAdded.getModel().getElementAt(selectedIx[i]);
    //str[i] = ""+element;
    str.add(element.toString());
 //   boolean add = stringSel.add(""+sel);
  }
  return(str);
}
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton1AddItem;
  private javax.swing.JButton jButton2AddItem;
  private javax.swing.JButton jButton3_plot;
  private javax.swing.JButton jButton4;
  private javax.swing.JButton jButton5RemoveItem;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextField xValueAdded;
  private javax.swing.JComboBox xValuesAvail;
  protected javax.swing.JList yValuesAdded;
  private javax.swing.JComboBox yValuesAvail;
  // End of variables declaration//GEN-END:variables
  public String yVariables;
  public String xVariables;
  private boolean firstItemAdded = false;
  private Error_duplicate_name duplicateNameDialog = null;

}
